/*
 * @描述: 分类管理抽取部分方法ts文件
 * @作者: 研发中心
 * @Date: 2019-12-06 15:02:32
 */
import vueAxios from "@/api/index";

/**
 * @名字:getCatalog
 * @详情:获取归档数据
 * @参数: 无
 * @返回值: 无
 */
export const getCatalog = (Fonds_ID: string) => {
  return new Promise((resolve, reject) => {
    const param = { order: "FC_ID asc", dtoList: [{ fieldName: "Fonds_ID", fieldValue: Fonds_ID, conditionalType: 0 }] };
    vueAxios.post("Master_Base_Fonds_Class/GetConditional", param, { noLoading: true }).then(res => {
      if (res.List.length > 0) {
        const param2 = {
          order: "FM_Index asc",
          dtoList: [{ fieldName: "FC_ID", fieldValue: res.List.map((v: any) => v.FC_ID).join(","), conditionalType: 6 }]
        };
        vueAxios.post("Master_Base_FormModel/GetConditional", param2, { noLoading: true }).then((res2: any) => {
          resolve(res2.List);
        });
      } else {
        resolve([]);
      }
    });
  });
};

/**
 * @名字:getTreeData
 * @详情:获取树数据
 * @参数: 无
 * @返回值: 无
 */
export const getTreeData = (FM_ID: string) => {
  return new Promise((resolve, reject) => {
    const param = {
      order: "Catalog_Order asc",
      dtoList: [{ fieldName: "FM_ID", fieldValue: FM_ID, conditionalType: 0 }]
    };
    vueAxios.post("Master_Base_Catalog/GetConditional", param).then((res: any) => {
      res.List.forEach((item: any) => {
        item.name = `${item.Catalog_Code} ${item.Catalog_Name}`;
        if (item.Catalog_Enable_Expand) {
          item.open = true;
        }
      });
      resolve(res.List);
    });
  });
};

/**
 * @名字:getBGQXData
 * @详情:获取保管期限数据
 * @参数: 无
 * @返回值: 无
 */
export const getBGQXData = () => {
  return new Promise((resolve, reject) => {
    vueAxios.get("Master_Base_DataDictionary/Get", { order: "DD_Order asc" }).then((res: any) => {
      if (res.Mark) {
        const param = {
          order: "DDL_Order asc",
          dtoList: [
            {
              fieldName: "DD_ID",
              fieldValue: res.List.filter((v: any) => v.DD_Code === "BGQXdata")[0].DD_ID,
              conditionalType: 0
            }
          ]
        };
        vueAxios.post("Master_Base_DDL/GetConditional", param, { noLoading: true }).then((res2: any) => {
          resolve(res2.List);
        });
      }
    });
  });
};
/**
 * @描述: 通过id和父级id转换为tree形结构源数据(用作导入操作)
 * @参数: data: 数据源, pid: 父级id
 * @返回值: 无
 */
export const convertToExportData = (data: any, pid: string | null) => {
  const result = [];
  let temp = [];
  for (let i = 0; i < data.length; i++) {
    if (data[i].Catalog_Father === pid) {
      const obj = { ...data[i] };
      temp = convertToExportData(data, data[i].Catalog_ID);
      if (temp.length > 0) {
        obj.children = temp;
      }
      result.push(obj);
    }
  }
  return result;
};
